<?php
/**
* 连接数据库
* @author steven
* @author seraph.liujb 2009.4.22
*/
class DB 
{
    
    /**
    * (static)取得数据库读连接
    * @return   Object
    */
    public static function getRDB()
    {
       $db = new self();
       return $db -> _rdb();
    }
    
    /**
    * (static)取得数据库写连接
    * @return   Object
    */
    public static function getWDB()
    {
    	$db = new self();
       	return $db -> _wdb();
    }
    
    /**
    * (static)取得数据库读写连接
    * @return   object
    */
    public static function getDB()
    {
       $db = new self();
       return $db -> _db();
    }
    
    /**
    * 读取配置文件
    * @param    string $type    
    * @return   object
    */
    private function getConfig($type) {
       $config = new Zend_Config_Ini(ROOT_PATH . '/application/_config/system.ini',$type);
       return $config;
    }
    
    /**
     * 初始化数据库连接对象
     *
     * @param string $type db rdb wdb，不同的数据库连接类型
     * @return object $db
     * @author seraph.liujb 2009.4.22
     */
    private function initDb($type) {
		$db = Zend_Registry::get($type);
		//切换UTF8
   		$db->query("set names 'UTF8';");
        //判断是否生产环境，打开数据库执行统计
        $fc = Zend_Controller_Front::getInstance();
        $env = $fc->getPlugin('Initializer')->getEnv();
		if ('dev' == $env || 'test' == $env) {
			$db->getProfiler()->setEnabled(true);
		}
		//
		return $db;
    }
    /**
     * 获取各数据库连接的Zend_Db_Profile，并返回统计数据值
     *
     * @author seraph.liujb 2009.4.22
     */
    public function getDbProfiler() {
    	$queryArray = array();
		$this->procProfiler('db', $queryArray);
//		$this->procProfiler('rdb', $queryArray);
//		$this->procProfiler('wdb', $queryArray);
		return $queryArray;
    }
	private function procProfiler($type, &$queryArray = array()) {
		switch ($type) {
			case 'rdb' :
				$db = $this->_rdb();
			break;
			case 'wdb' :
				$db = $this->_wdb();
			break;
			default:
				$db = $this->_db();
			break;
		}
    	$profiler = $db->getProfiler();
    	if (!$profiler->getQueryProfiles()) {
    		return $queryArray;
    	}
		foreach ($profiler->getQueryProfiles() as $qp) {
			$qm = array();
			$qm['sqlText'] = $qp->getQuery();
			$qm['sqlTime'] = $qp->getElapsedSecs();
			$qm['dbType'] = $type;
			$queryArray[] = $qm;
		}
		//生成total统计
		$queryArray['sqlTimeTotal'] += $profiler->getTotalElapsedSecs();
		$queryArray['sqlCount'] += $profiler->getTotalNumQueries();
	}
    
    /**
    * 建立数据库读连接
    * @return   object
    */
    private function _rdb()
    {
    	$type = 'rdb';
    	if(Zend_Registry::isRegistered($type)){
    		return Zend_Registry::get($type);
    	}else{
    		$config = $this->getConfig($type);
       		$db = Zend_Db::factory($config->adapter,$config);
       		Zend_Registry::set($type,$db);
       		$this->initDb($type);
       		return $db;
    	}
    }
    
    /**
    * 建立数据库写连接
    * @return   object
    */
    private function _wdb()
    {
    	$type = 'wdb';
    	if(Zend_Registry::isRegistered($type)){
    		return Zend_Registry::get($type);
    	}else{
    		$config = $this->getConfig($type);
       		$db = Zend_Db::factory($config->adapter,$config);
       		Zend_Registry::set($type,$db);
       		$this->initDb($type);
       		return $db;
    	}
    }
    
    /**
    * 建立数据库读写连接
    * @return   object
    */
    private function _db()
    {
    	$type = 'db';
    	if(Zend_Registry::isRegistered($type)){
    		return Zend_Registry::get($type);
    	}else{
    		$config = $this->getConfig($type);
    		$db = Zend_Db::factory($config->adapter,$config);
    		Zend_Registry::set($type,$db);
    		$this->initDb($type);
       		return $db;
    	}
    }
}
